<?php

namespace addons\addondev;

use addons\addondev\library\ClassLoader;
use app\common\library\Menu;
use think\Addons;

/**
 * 插件
 */
class Addondev extends Addons
{



    /**
     * 插件安装方法
     *
     * @return bool
     */
    public function install()
    {
        $menu = [
            [
                'name' => 'addondev',
                'title' => '插件开发代码生成器',
                'icon' => 'fa fa-magic',
                'sublist' =>
                [
                    [
                        'name' => 'addondev/gen',
                        'title' => '代码管理',
                        'icon' => 'fa fa-code',
                        'remark' => '提示生产环境务必禁用此插件并关闭调试模式',
                        'weigh' => 2,
                        'sublist' =>
                        [
                            [
                                'name' => 'addondev/gen/edit',
                                'title' => '编辑生成代码',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                            [
                                'name' => 'addondev/gen/code',
                                'title' => '预览代码',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                            [
                                'name' => 'addondev/gen/diff',
                                'title' => '对比代码',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                            [
                                'name' => 'addondev/gen/exportmenu',
                                'title' => '导出菜单',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                            [
                                'name' => 'addondev/gen/check',
                                'title' => '验证代码依赖',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                            [
                                'name' => 'addondev/gen/index',
                                'title' => '查看',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                            [
                                'name' => 'addondev/gen/add',
                                'title' => '添加',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                            [
                                'name' => 'addondev/gen/del',
                                'title' => '删除',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                            [
                                'name' => 'addondev/gen/multi',
                                'title' => '批量更新',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                        ],
                    ],
                    [
                        'name' => 'addondev/log',
                        'title' => '代码文件日志',
                        'icon' => 'fa fa-history',
                        'remark' => '记录被代码生成器覆盖前的文件，可以预览，对比本地代码，恢复代码;提示生产环境务必禁用此插件并关闭调试模式',
                        'weigh' => 1,
                        'sublist' =>
                        [
                            [
                                'name' => 'addondev/log/index',
                                'title' => '查看',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                            [
                                'name' => 'addondev/log/del',
                                'title' => '删除',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                            [
                                'name' => 'addondev/log/multi',
                                'title' => '批量更新',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                            [
                                'name' => 'addondev/log/diff',
                                'title' => '对比本地代码',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                            [
                                'name' => 'addondev/log/code',
                                'title' => '预览代码',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                            [
                                'name' => 'addondev/log/recover',
                                'title' => '回复文件',
                                'icon' => 'fa fa-circle-o',
                                'weigh' => 0,
                            ],
                        ],
                    ],
                ],
            ],
        ];
        Menu::create($menu);
        return true;
    }

    /**
     * 插件卸载方法
     *
     * @return bool
     */
    public function uninstall()
    {
        Menu::delete('addondev');
        return true;
    }

    /**
     * 插件启用方法
     */
    public function enable()
    {
        Menu::enable('addondev');
    }

    /**
     * 插件禁用方法
     */
    public function disable()
    {
        Menu::disable('addondev');
    }

    /**
     * 应用初始化
     */
    public function appInit()
    {

        $libpath = ADDON_PATH . 'addondev' . DS . 'library' . DS;
        ClassLoader::addPsr0("Diff", $libpath . "PhpDiff" . DS, true);

        if (request()->isCli()) {
            \think\Console::addDefaultCommands([
                'addons\addondev\command\Addondev',
                'addons\addondev\command\Addoncrud'
            ]);
        }
    }
}
